"""
报告生成状态管理
"""
from typing import Dict, List, Optional, Any
from dataclasses import dataclass, field
from enum import Enum

class AgentStatus(Enum):
    """代理状态枚举"""
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"

@dataclass
class SectionContent:
    """章节内容"""
    title: str
    content: str
    code_blocks: List[str] = field(default_factory=list)
    images: List[str] = field(default_factory=list)
    references: List[str] = field(default_factory=list)

@dataclass
class ReportStructure:
    """报告结构"""
    title: str
    abstract: str
    sections: List[Dict[str, Any]] = field(default_factory=list)
    conclusion: str = ""
    references: List[str] = field(default_factory=list)

class ReportState:
    """报告生成状态类"""
    
    def __init__(self):
        # 基本信息
        self.requirement: str = ""
        self.task_type: str = "report_generation"
        self.priority: str = "normal"
        
        # 报告结构
        self.report_structure: Optional[ReportStructure] = None
        self.sections_content: Dict[str, SectionContent] = {}
        
        # 代理状态
        self.current_agent: str = "planner"
        self.agent_status: Dict[str, AgentStatus] = {
            "planner": AgentStatus.PENDING,
            "writer": AgentStatus.PENDING,
            "coder": AgentStatus.PENDING,
            "verifier": AgentStatus.PENDING,
            "reviewer": AgentStatus.PENDING
        }
        
        # 处理结果
        self.final_report: str = ""
        self.error_messages: List[str] = []
        self.processing_logs: List[str] = []
        
        # 质量评估
        self.quality_score: float = 0.0
        self.review_feedback: List[str] = []
        
    def add_log(self, message: str, agent: str = "system"):
        """添加处理日志"""
        log_entry = f"[{agent}] {message}"
        self.processing_logs.append(log_entry)
        
    def add_error(self, error: str, agent: str = "system"):
        """添加错误信息"""
        error_entry = f"[{agent}] {error}"
        self.error_messages.append(error_entry)
        
    def update_agent_status(self, agent: str, status: AgentStatus):
        """更新代理状态"""
        if agent in self.agent_status:
            self.agent_status[agent] = status
            self.add_log(f"代理 {agent} 状态更新为: {status.value}")
            
    def set_current_agent(self, agent: str):
        """设置当前处理的代理"""
        self.current_agent = agent
        self.add_log(f"切换到代理: {agent}")
        
    def is_completed(self) -> bool:
        """检查是否所有代理都已完成"""
        return all(status == AgentStatus.COMPLETED for status in self.agent_status.values())
        
    def has_errors(self) -> bool:
        """检查是否有错误"""
        return len(self.error_messages) > 0
        
    def get_progress(self) -> float:
        """获取整体进度"""
        completed_count = sum(1 for status in self.agent_status.values() 
                            if status == AgentStatus.COMPLETED)
        total_count = len(self.agent_status)
        return (completed_count / total_count) * 100
        
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            "requirement": self.requirement,
            "task_type": self.task_type,
            "priority": self.priority,
            "current_agent": self.current_agent,
            "agent_status": {k: v.value for k, v in self.agent_status.items()},
            "progress": self.get_progress(),
            "quality_score": self.quality_score,
            "has_errors": self.has_errors(),
            "error_count": len(self.error_messages),
            "log_count": len(self.processing_logs)
        } 